package org.itzheng.itplayer.local

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.itzheng.base.BaseFragment
import org.itzheng.itplayer.R
import org.itzheng.itplayer.adapter.VideoListPreviewAdapter
import org.itzheng.itplayer.bean.VideoInfo

import org.itzheng.setGridAdapter
import org.itzheng.itplayer.player.VideoPlayActivity
import java.io.Serializable
import java.util.*

/**
 * Title:文件列表<br></br>
 * Description: <br></br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2020-12-4.
 */
class LocalVideoFileFragment : BaseFragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflate(R.layout.fragment_recycler_view)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setAdapter(getVideoList(arguments))
    }

    private fun setAdapter(videoList: ArrayList<VideoInfo>?) {
        val adapter: VideoListPreviewAdapter = VideoListPreviewAdapter()
        adapter.setOnItemClickListener { view, position ->
//            val item = adapter.getItem(position) as VideoInfo
            VideoPlayActivity.startActivity(activity!!, videoList, position)

        }
        rootView.findViewById<RecyclerView>(R.id.rcvContent).setGridAdapter(adapter, 2)
        adapter.submitList(videoList)
    }

    companion object {
        private const val KEY_LIST_VIDEO_INFO = "KEY_LIST_VIDEO_INFO"
        fun newInstance(items: MutableList<VideoInfo>): LocalVideoFileFragment {
            val args = Bundle()
            args.putSerializable(KEY_LIST_VIDEO_INFO, items as Serializable)
            val fragment = LocalVideoFileFragment()
            fragment.arguments = args
            return fragment
        }

        fun getVideoList(arguments: Bundle?): ArrayList<VideoInfo>? {
            val obj = arguments!!.getSerializable(KEY_LIST_VIDEO_INFO) ?: return null
            return obj as ArrayList<VideoInfo>
        }
    }
}